Skip to content

Commit 6986cb0

Browse files
committed
Adjust flags / traits changed
1 parent 68fe0e8 commit 6986cb0

19 files changed

+188
-127
lines changed

Diff for: flagsmith-core.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
} from './types';
1313
// @ts-ignore
1414
import deepEqual from 'fast-deep-equal';
15-
import { AsyncStorageType } from './async-storage';
15+
import { AsyncStorageType } from './utils/async-storage';
16+
import getChanges from './utils/get-changes';
1617

1718
enum FlagSource {
1819
"NONE" = "NONE",
@@ -177,8 +178,8 @@ const Flagsmith = class {
177178
this.oldFlags = {
178179
...this.flags
179180
};
180-
const flagsEqual = deepEqual(this.flags, flags);
181-
const traitsEqual = deepEqual(this.traits, userTraits);
181+
const flagsChanged = getChanges(this.oldFlags, flags);
182+
const traitsChanged = getChanges(this.withTraits, userTraits);
182183
this.flags = flags;
183184
this.traits = userTraits;
184185
this.updateStorage();
@@ -232,8 +233,8 @@ const Flagsmith = class {
232233
}
233234
this._onChange!(this.oldFlags, {
234235
isFromServer: true,
235-
flagsChanged: !flagsEqual,
236-
traitsChanged: !traitsEqual
236+
flagsChanged,
237+
traitsChanged
237238
}, this._loadedState(null, FlagSource.SERVER));
238239
};
239240

@@ -574,6 +575,8 @@ const Flagsmith = class {
574575
if (AsyncStorage && this.canUseStorage) {
575576
const onRetrievedStorage = (err: Error|null, res: string|null) => {
576577
if (res) {
578+
let flagsChanged = null
579+
let traitsChanged = null
577580
try {
578581
const json = JSON.parse(res);
579582
let cachePopulated = false;
@@ -593,6 +596,8 @@ const Flagsmith = class {
593596
}
594597
if (setState) {
595598
cachePopulated = true;
599+
traitsChanged = getChanges(this.traits, json.traits)
600+
flagsChanged = getChanges(this.flags, json.flags)
596601
this.setState(json);
597602
this.log("Retrieved flags from cache", json);
598603
}
@@ -601,7 +606,7 @@ const Flagsmith = class {
601606
if (cachePopulated) { // retrieved flags from local storage
602607
const shouldFetchFlags = !preventFetch && (!this.cacheOptions.skipAPI||!cachePopulated)
603608
this._onChange!(null,
604-
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
609+
{ isFromServer: false, flagsChanged, traitsChanged },
605610
this._loadedState(null, FlagSource.CACHE, shouldFetchFlags)
606611
);
607612
this.oldFlags = this.flags;
@@ -628,12 +633,12 @@ const Flagsmith = class {
628633
} else {
629634
if (defaultFlags) {
630635
this._onChange!(null,
631-
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
636+
{ isFromServer: false, flagsChanged: getChanges({}, this.flags), traitsChanged: getChanges({}, this.traits) },
632637
this._loadedState(null, FlagSource.DEFAULT_FLAGS)
633638
);
634639
} else if (this.flags) { // flags exist due to set state being called e.g. from nextJS serverState
635640
this._onChange?.(null,
636-
{ isFromServer: false, flagsChanged: true, traitsChanged: !!this.traits && !!Object.keys(this.traits).length },
641+
{ isFromServer: false, flagsChanged: getChanges({}, this.flags), traitsChanged: getChanges({}, this.traits) },
637642
this._loadedState(null, FlagSource.DEFAULT_FLAGS)
638643
);
639644
} else {
@@ -656,13 +661,13 @@ const Flagsmith = class {
656661
this.getFlags(resolve, reject);
657662
} else {
658663
if (defaultFlags) {
659-
this._onChange?.(null, { isFromServer: false, flagsChanged: true, traitsChanged:!!this.traits && !!Object.keys(this.traits).length },this._loadedState(null, FlagSource.DEFAULT_FLAGS));
660-
}else if (this.flags) {
664+
this._onChange?.(null, { isFromServer: false, flagsChanged: getChanges({}, defaultFlags), traitsChanged:getChanges({}, traits) },this._loadedState(null, FlagSource.DEFAULT_FLAGS));
665+
} else if (this.flags) {
661666
let error = null
662667
if(Object.keys(this.flags).length === 0){
663668
error = WRONG_FLAGSMITH_CONFIG
664669
}
665-
this._onChange?.(null, { isFromServer: false, flagsChanged: true, traitsChanged:!!this.traits && !!Object.keys(this.traits).length },this._loadedState(error, FlagSource.DEFAULT_FLAGS));
670+
this._onChange?.(null, { isFromServer: false, flagsChanged: getChanges({}, this.flags), traitsChanged:getChanges({}, traits) },this._loadedState(error, FlagSource.DEFAULT_FLAGS));
666671

667672
}
668673
resolve(true);
@@ -674,7 +679,7 @@ const Flagsmith = class {
674679
});
675680
}
676681

677-
_loadedState(error=null, source:FlagSource, isFetching=false) {
682+
_loadedState(error:any=null, source:FlagSource, isFetching=false) {
678683
return {
679684
error,
680685
isFetching,

Diff for: index-es.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IFlagsmith } from './types';
22

33
import fetch from "unfetch"
4-
import AsyncStorage from "./async-storage";
4+
import AsyncStorage from "./utils/async-storage";
55
import core, { LikeFetch } from './flagsmith-core';
66
// @ts-ignore
77
globalThis.FlagsmithEventSource = typeof EventSource!== "undefined"? EventSource: null;

Diff for: index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { IFlagsmith } from './types';
44
globalThis.FlagsmithEventSource = typeof EventSource!== "undefined"? EventSource: null;
55

66
import fetch from "unfetch"
7-
import AsyncStorage from "./async-storage";
7+
import AsyncStorage from "./utils/async-storage";
88
import core, { LikeFetch } from './flagsmith-core';
99
import _EventSource from 'reconnecting-eventsource'
1010
// @ts-expect-error

Diff for: isomorphic-es.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import AsyncStorage from "./async-storage";
1+
import AsyncStorage from "./utils/async-storage";
22
import {IFlagsmith} from "./types";
33
// @ts-expect-error
44
globalThis.FlagsmithEventSource = typeof EventSource !== 'undefined' ? EventSource : null;

Diff for: isomorphic.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import AsyncStorage from "./async-storage";
1+
import AsyncStorage from "./utils/async-storage";
22
import {IFlagsmith} from "./types";
33
import core from './flagsmith-core'
44

Diff for: lib/flagsmith-es/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith-es",
3-
"version": "3.24.0",
3+
"version": "3.25.0",
44
"description": "Feature flagging to support continuous development. This is an esm equivalent of the standard flagsmith npm module.",
55
"main": "./index.js",
66
"type": "module",

Diff for: lib/flagsmith/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "3.24.0",
3+
"version": "3.25.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

Diff for: lib/react-native-flagsmith/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "3.24.0",
3+
"version": "3.25.0",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

Diff for: react.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
useRef,
99
useState,
1010
} from 'react';
11-
import Emitter from './emitter';
11+
import Emitter from './utils/emitter';
1212
const events = new Emitter();
1313

1414
import { IFlagsmith, IFlagsmithTrait, IFlagsmithFeature, IState } from './types'
@@ -111,7 +111,7 @@ export function useFlagsmithLoading() {
111111
}
112112

113113
useEffect(() => {
114-
if (!subscribed && flagsmith.initialised) {
114+
if (!subscribed && flagsmith?.initialised) {
115115
events.on('loading_event', eventListener)
116116
setSubscribed(true)
117117
}

0 commit comments

Comments
 (0)