Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: feat: batchRelink to return old ProxyState #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/StateTrackerContext.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import StateTrackerNode from './StateTrackerNode';
import { StateTrackerInterface } from './types';
import { generateRandomKey } from './commons';

class StateTrackerContext {
private queue: Array<StateTrackerNode>;
private _trackerMap: Map<string, StateTrackerInterface>;
private _lastUpdateAt: number;
private _id: string;
private _backTrackingEnabled: boolean;

constructor() {
this.queue = [];
this._id = generateRandomKey();
this._trackerMap = new Map();
this._lastUpdateAt = Date.now();
this._backTrackingEnabled = true;
}

getId() {
return this._id;
}

getTrackerMap(): Map<string, StateTrackerInterface> {
Expand Down Expand Up @@ -56,6 +65,18 @@ class StateTrackerContext {
getTime(): number {
return this._lastUpdateAt;
}

enableBackTracking() {
this._backTrackingEnabled = true;
}

disableBackTracking() {
this._backTrackingEnabled = false;
}

getBackTrackingState(): boolean {
return this._backTrackingEnabled;
}
}

export default StateTrackerContext;
16 changes: 15 additions & 1 deletion src/commons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IStateTracker } from './types';
import { IStateTracker, SeenKeys } from './types';

const toString = Function.call.bind<Function>(Object.prototype.toString);
const ownKeys = (o: any) =>
Expand Down Expand Up @@ -119,3 +119,17 @@ export const peek = (proxyState: IStateTracker, accessPath: Array<string>) => {
return nextProxy;
}, proxyState);
};

const seenKeys: SeenKeys = {};
const MULTIPLIER = Math.pow(2, 24) // eslint-disable-line

export const generateRandomKey = () => {
let key;

while (key === undefined || seenKeys.hasOwnProperty(key) || !isNaN(+key)) { // eslint-disable-line
key = Math.floor(Math.random() * MULTIPLIER).toString(32);
}

seenKeys[key] = true;
return key;
};
47 changes: 47 additions & 0 deletions src/es5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ProduceOptions,
ProduceState,
IndexType,
RelinkValue,
} from './types';
import StateTrackerContext from './StateTrackerContext';

Expand Down Expand Up @@ -402,6 +403,52 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
const stateContext = tracker._stateTrackerContext;
stateContext.updateTime();
});
createHiddenProperty(state, 'batchRelink', function(
this: IStateTracker,
values: Array<RelinkValue>
) {
const tracker = this[TRACKER];
const baseValue = Object.assign({}, tracker.getBase());
const stackerTrackerContext = new StateTrackerContext();

// should create a new object....
const newTracker = new StateTracker({
base: baseValue,
parentProxy: tracker.getParentProxy(),
accessPath: tracker.getAccessPath(),
rootPath: tracker.getRootPath(),
stateTrackerContext: stackerTrackerContext,
context: tracker.getContext(),
lastUpdateAt: Date.now(),
focusKey: null,
});

const proxyStateCopy = produce(
{ ...baseValue },
{
// parentProxy: null,
accessPath: [],
rootPath: [],
stateTrackerContext: stackerTrackerContext,
mayReusedTracker: newTracker,
context: tracker.getContext(),
focusKey: null,
}
);

const childProxies = Object.assign({}, tracker.getChildProxies());

values.forEach(({ path, value }) => {
this.relink(path, value);

// unchanged object's proxy object will be preserved.
delete childProxies[path[0]];
});

newTracker.setChildProxies(childProxies);

return proxyStateCopy;
});
createHiddenProperty(state, 'unlink', function(this: IStateTracker) {
const tracker = this[TRACKER];
return tracker.getShadowBase();
Expand Down
89 changes: 78 additions & 11 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import invariant from 'invariant';
import {
peek,
TRACKER,
isObject,
isTrackable,
isTypeEqual,
createHiddenProperty,
Expand All @@ -13,6 +14,7 @@ import {
ProduceOptions,
ProduceState,
StateTrackerInterface,
RelinkValue,
} from './types';
import StateTrackerContext from './StateTrackerContext';

Expand All @@ -34,6 +36,7 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
'enter',
'leave',
'getContext',
'batchRelink',
'relink',
'unlink',
'hydrate',
Expand Down Expand Up @@ -67,8 +70,9 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
if (trackerContext.getCurrent()) {
trackerContext.getCurrent().reportPaths(nextAccessPath);
}
const currentTrackerContext = trackerContext.getCurrent();

if (trackerContext.getCurrent()) {
if (currentTrackerContext && trackerContext.getBackTrackingState()) {
const stateContextNode = trackerContext.getCurrent();
const { context } = stateContextNode;
const internalContext = tracker.getContext();
Expand Down Expand Up @@ -100,7 +104,12 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {

let value;
let baseTracker;
if (typeof (baseTracker = base.getTracker()) !== 'undefined') {
// if (typeof (baseTracker = base.getTracker()) !== 'undefined') {
if (
isObject(base) &&
base.getTracker &&
typeof (baseTracker = base.getTracker()) !== 'undefined'
) {
baseTracker.setStrictPeeking(true);
value = base[prop];
baseTracker.setStrictPeeking(false);
Expand All @@ -123,15 +132,22 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
const childProxy = childProxies[prop as string];

// for rebase value, if base value change, the childProxy should be replaced
let childProxyTracker = null;
let mayReusedTracker = null;

if (childProxy) {
childProxyTracker = childProxy[TRACKER];
const childProxyBase = childProxyTracker.getBase();
if (childProxyBase === value) {
if (tracker._context)
childProxyTracker.setContext(tracker._context);
return childProxy;
const candidateChildProxyTracker = childProxy[TRACKER];
// only if
if (
candidateChildProxyTracker.getStateTrackerContext().getId() ===
tracker.getStateTrackerContext().getId()
) {
mayReusedTracker = candidateChildProxyTracker;
const childProxyBase = mayReusedTracker.getBase();
if (childProxyBase === value) {
if (tracker._context)
mayReusedTracker.setContext(tracker._context);
return childProxy;
}
}
}

Expand Down Expand Up @@ -188,7 +204,7 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
accessPath: nextAccessPath,
parentProxy: proxy as IStateTracker,
rootPath,
mayReusedTracker: childProxyTracker,
mayReusedTracker,
stateTrackerContext: trackerContext,
context: tracker._context,
focusKey: prop as string,
Expand Down Expand Up @@ -245,6 +261,7 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
lastUpdateAt: Date.now(),
focusKey,
});

trackerContext.setTracker(mapKey, tracker);

const proxy = new Proxy(state, handler);
Expand All @@ -266,16 +283,66 @@ function produce(state: ProduceState, options?: ProduceOptions): IStateTracker {
this: IStateTracker,
path: Array<string>,
value: any
// proxyState?: IStateTracker
) {
const copy = path.slice();
const last = copy.pop();
const front = copy;
const parentState = peek(this, front);
parentState[last!] = value;

const tracker = this[TRACKER];
const stateContext = tracker._stateTrackerContext;
stateContext.updateTime();
parentState[last!] = value;
});

createHiddenProperty(proxy, 'batchRelink', function(
this: IStateTracker,
values: Array<RelinkValue>
) {
const tracker = this[TRACKER];
const baseValue = Object.assign({}, tracker.getBase());
const stackerTrackerContext = new StateTrackerContext();

// should create a new object....
const newTracker = new StateTracker({
base: baseValue,
parentProxy: tracker.getParentProxy(),
accessPath: tracker.getAccessPath(),
rootPath: tracker.getRootPath(),
stateTrackerContext: stackerTrackerContext,
context: tracker.getContext(),
lastUpdateAt: Date.now(),
focusKey: null,
});

const proxyStateCopy = produce(
{ ...baseValue },
{
// parentProxy: null,
accessPath: [],
rootPath: [],
stateTrackerContext: stackerTrackerContext,
mayReusedTracker: newTracker,
context: tracker.getContext(),
focusKey: null,
}
);

const childProxies = Object.assign({}, tracker.getChildProxies());

values.forEach(({ path, value }) => {
this.relink(path, value);

// unchanged object's proxy object will be preserved.
delete childProxies[path[0]];
});

newTracker.setChildProxies(childProxies);

return proxyStateCopy;
});

createHiddenProperty(proxy, 'unlink', function(this: IStateTracker) {
const tracker = this[TRACKER];
return tracker.getBase();
Expand Down
4 changes: 4 additions & 0 deletions src/types/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ export enum Type {
}

export type IndexType = string | number;

export interface SeenKeys {
[key: string]: boolean;
}
5 changes: 5 additions & 0 deletions src/types/produce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export type Produce = (
state: ProduceState,
options?: ProduceOptions
) => IStateTracker;

export type RelinkValue = {
path: Array<string>;
value: any;
};
2 changes: 2 additions & 0 deletions src/types/stateTracker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Type } from './';
import { TRACKER } from '../commons';
import StateTrackerContext from '../StateTrackerContext';
import { RelinkValue } from './produce';

export interface ProxyStateTrackerConfig {
accessPath?: Array<string>;
Expand Down Expand Up @@ -94,6 +95,7 @@ export interface IStateTracker {
enter(context?: string): void;
leave(): void;
relink(path: Array<String>, value: any): void;
batchRelink(values: Array<RelinkValue>): IStateTracker;
unlink(): any;
hydrate(path: Array<String>, value: any): void;
getContext(): StateTrackerContext;
Expand Down