Skip to content

Commit af7b676

Browse files
authored
Merge pull request #302 from Flagsmith/fix/this-identity-reverse-compatibility
fix: this.identity reverse compatibility
2 parents bd2e5c8 + a33385a commit af7b676

File tree

6 files changed

+16
-3
lines changed

6 files changed

+16
-3
lines changed

flagsmith-core.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ const Flagsmith = class {
137137
};
138138
if (identifier) {
139139
this.evaluationContext.identity.identifier = identifier;
140+
this.identity = identifier;
140141
}
141142
}
142143
this.flags = flags;
@@ -277,6 +278,7 @@ const Flagsmith = class {
277278
flags:IFlags|null= null
278279
getFlagInterval: NodeJS.Timer|null= null
279280
headers?: object | null= null
281+
identity:string|null|undefined = null
280282
initialised= false
281283
oldFlags:IFlags|null= null
282284
onChange:IInitConfig['onChange']|null= null
@@ -439,7 +441,7 @@ const Flagsmith = class {
439441
const onRetrievedStorage = async (error: Error | null, res: string | null) => {
440442
if (res) {
441443
let flagsChanged = null
442-
let traitsChanged = null
444+
const traitsChanged = null
443445
try {
444446
const json = JSON.parse(res) as IState;
445447
let cachePopulated = false;
@@ -563,6 +565,7 @@ const Flagsmith = class {
563565
}
564566

565567
identify(userId?: string | null, traits?: ITraits, transient?: boolean) {
568+
this.identity = userId
566569
this.evaluationContext.identity = {
567570
identifier: userId,
568571
transient: transient,
@@ -591,6 +594,7 @@ const Flagsmith = class {
591594
flags: this.flags,
592595
ts: this.ts,
593596
evaluationContext: this.evaluationContext,
597+
identity: this.identity,
594598
evaluationEvent: this.evaluationEvent,
595599
} as IState
596600
}
@@ -602,11 +606,13 @@ const Flagsmith = class {
602606
this.flags = state.flags || this.flags;
603607
this.evaluationContext = state.evaluationContext || this.evaluationContext,
604608
this.evaluationEvent = state.evaluationEvent || this.evaluationEvent;
609+
this.identity = this.getContext()?.identity?.identifier
605610
this.log("setState called", this)
606611
}
607612
}
608613

609614
logout() {
615+
this.identity = null
610616
this.evaluationContext.identity = null;
611617
if (this.initialised) {
612618
return this.getFlags();
@@ -676,6 +682,7 @@ const Flagsmith = class {
676682
...evaluationContext,
677683
environment: evaluationContext.environment || this.evaluationContext.environment,
678684
};
685+
this.identity = this.getContext()?.identity?.identifier
679686

680687
if (this.initialised) {
681688
return this.getFlags();

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "9.2.1",
3+
"version": "9.2.2",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"module": "./index.mjs",

lib/react-native-flagsmith/package.json

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

test/init.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe('Flagsmith.init', () => {
7878
);
7979
expect(getStateToCheck(flagsmith.getState())).toEqual({
8080
...identityState,
81+
identity: testIdentityWithTraits,
8182
evaluationContext: {
8283
...identityState.evaluationContext,
8384
identity: {
@@ -135,7 +136,9 @@ describe('Flagsmith.init', () => {
135136
status: 200,
136137
text: () => fs.readFile(`./test/data/identities_${identityB}.json`, 'utf8'),
137138
});
139+
expect(flagsmith.identity).toEqual(identityA);
138140
await flagsmith.identify(identityB);
141+
expect(flagsmith.identity).toEqual(identityB);
139142
expect(flagsmith.getTrait('a')).toEqual(undefined);
140143
mockFetch.mockResolvedValueOnce({
141144
status: 200,

test/test-constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const defaultState = {
2727
export const testIdentity = 'test_identity'
2828
export const identityState = {
2929
api: 'https://edge.api.flagsmith.com/api/v1/',
30+
identity: testIdentity,
3031
evaluationContext: {
3132
environment: {apiKey: environmentID},
3233
identity: {
@@ -63,6 +64,7 @@ export const defaultStateAlt = {
6364
export function getStateToCheck(_state: IState) {
6465
const state = {
6566
..._state,
67+
identity: _state.evaluationContext?.identity?.identifier,
6668
};
6769
delete state.evaluationEvent;
6870
// @ts-ignore internal property

types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface IState<F extends string = string> {
5353
evaluationContext?: EvaluationContext;
5454
evaluationEvent?: Record<string, Record<string, number>> | null;
5555
ts?: number;
56+
identity?: string;
5657
}
5758

5859
declare type ICacheOptions = {

0 commit comments

Comments
 (0)