Skip to content

Commit

Permalink
Merge pull request #47 from dhilt/issue-44-adapter-reset-reload-colli…
Browse files Browse the repository at this point in the history
…sion

Adapter.reset context persistence
  • Loading branch information
dhilt committed May 8, 2023
2 parents c564ea0 + ddfe19d commit 4b1b433
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
60 changes: 21 additions & 39 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vscroll",
"version": "1.5.3",
"version": "1.5.4",
"description": "Virtual scroll engine",
"main": "dist/bundles/vscroll.umd.js",
"module": "dist/bundles/vscroll.esm5.js",
Expand Down Expand Up @@ -65,4 +65,4 @@
"javascript",
"typescript"
]
}
}
26 changes: 24 additions & 2 deletions src/classes/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ import {
} from '../interfaces/index';

type MethodResolver = (...args: unknown[]) => Promise<AdapterMethodResult>;
type InitializationParams<Item> = {
buffer: Buffer<Item>,
state: State,
viewport: Viewport,
logger: Logger,
adapterRun$?: Reactive<ProcessSubject>,
getWorkflow?: WorkflowGetter<Item>
}

const ADAPTER_PROPS_STUB = getDefaultAdapterProps();

Expand Down Expand Up @@ -63,6 +71,7 @@ export class Adapter<Item = unknown> implements IAdapter<Item> {
private source: { [key: string]: Reactive<unknown> } = {}; // for Reactive props
private box: { [key: string]: unknown } = {}; // for Scalars over Reactive props
private demand: { [key: string]: unknown } = {}; // for Scalars on demand
private disposed: boolean;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
setFirstOrLastVisible = (_: { first?: boolean, last?: boolean, workflow?: ScrollerWorkflow }) => { };
Expand Down Expand Up @@ -264,7 +273,7 @@ export class Adapter<Item = unknown> implements IAdapter<Item> {
}

initialize(
buffer: Buffer<Item>, state: State, viewport: Viewport, logger: Logger, adapterRun$?: Reactive<ProcessSubject>
{ buffer, state, viewport, logger, adapterRun$, getWorkflow }: InitializationParams<Item>
): void {
// buffer
Object.defineProperty(this.demand, AdapterPropName.itemsCount, {
Expand Down Expand Up @@ -344,6 +353,11 @@ export class Adapter<Item = unknown> implements IAdapter<Item> {
});
}

// workflow getter
if (getWorkflow) {
this.getWorkflow = getWorkflow;
}

// init
this.init = true;
}
Expand All @@ -358,6 +372,7 @@ export class Adapter<Item = unknown> implements IAdapter<Item> {
Object.getOwnPropertyNames(this).forEach(prop => {
delete (this as Record<string, unknown>)[prop];
});
this.disposed = true;
}

resetContext(): void {
Expand Down Expand Up @@ -511,8 +526,15 @@ export class Adapter<Item = unknown> implements IAdapter<Item> {
resolve(false);
});
}).then(immediate => {
if (this.disposed) {
return {
immediate,
success: false,
details: 'Adapter was disposed'
};
}
const success = reloadId === this.reloadId;
this.logger.log(() => !success ? `relax promise cancelled due to ${reloadId} != ${this.reloadId}` : void 0);
this.logger?.log?.(() => !success ? `relax promise cancelled due to ${reloadId} != ${this.reloadId}` : void 0);
return {
immediate,
success,
Expand Down
1 change: 0 additions & 1 deletion src/processes/adapter/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export default class Reload extends BaseAdapterProcessFactory(AdapterProcess.rel

const payload: ProcessPayload = {};
if (state.cycle.busy.get()) {
state.scroll.stop();
payload.finalize = true;
state.cycle.interrupter = Reload.process;
}
Expand Down
8 changes: 7 additions & 1 deletion src/scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ export class Scroller<Data = unknown> {
init(adapterRun$?: Reactive<ProcessSubject>): void {
this.viewport.reset(this.buffer.startIndex);
this.logger.stat('initialization');
this.adapter.initialize(this.buffer, this.state, this.viewport, this.logger, adapterRun$);
this.adapter.initialize({
buffer: this.buffer,
state: this.state,
viewport: this.viewport,
logger: this.logger, adapterRun$,
getWorkflow: () => this.workflow
});
}

dispose(forever?: boolean): void {
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
name: 'vscroll',
version: '1.5.3'
version: '1.5.4'
};
12 changes: 9 additions & 3 deletions src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,27 @@ export class Workflow<ItemData = unknown> {
const { workflow, logger } = this.scroller;
// we are going to create a new reference for the scroller.workflow object
// calling the old version of the scroller.workflow by any outstanding async processes will be skipped
workflow.call = (p: ProcessSubject) => // eslint-disable-line @typescript-eslint/no-unused-vars
workflow.call = (_: ProcessSubject) => // eslint-disable-line @typescript-eslint/no-unused-vars
logger.log('[skip wf call]');
workflow.call.interrupted = true;
this.scroller.workflow = this.getUpdater();
this.interruptionCount++;
logger.log(() => `workflow had been interrupted by the ${process} process (${this.interruptionCount})`);
}
if (datasource) { // Scroller re-initialization case
this.scroller.adapter.relax(() => {
const reInit = () => {
this.scroller.logger.log('new Scroller instantiation');
const scroller = new Scroller<ItemData>({ datasource, scroller: this.scroller });
this.scroller.dispose();
this.scroller = scroller;
this.scroller.init();
});
};
if (this.scroller.state.cycle.busy.get()) {
// todo: think about immediate re-initialization even is there are pending processes
this.scroller.adapter.relax(reInit.bind(this));
} else {
reInit();
}
}
}

Expand Down

0 comments on commit 4b1b433

Please sign in to comment.