Skip to content

Commit

Permalink
new Immer({autoFreeze: false});
Browse files Browse the repository at this point in the history
  • Loading branch information
wighawag committed Nov 25, 2023
1 parent fb27b3e commit 4cd00f5
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 25 deletions.
7 changes: 7 additions & 0 deletions examples/event-processor-bleeps/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# event-processor-bleeps

## 0.0.43

### Patch Changes

- Updated dependencies
- ethereum-indexer-js-processor@0.6.17

## 0.0.42

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-bleeps/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-bleeps",
"private": true,
"version": "0.0.42",
"version": "0.0.43",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
7 changes: 7 additions & 0 deletions examples/event-processor-conquest-eth/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# event-processor-conquest-eth

## 0.0.43

### Patch Changes

- Updated dependencies
- ethereum-indexer-js-processor@0.6.17

## 0.0.42

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-conquest-eth/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-conquest-eth",
"private": true,
"version": "0.0.42",
"version": "0.0.43",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
7 changes: 7 additions & 0 deletions examples/event-processor-conquest-fplay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# event-processor-conquest-fplay

## 0.0.43

### Patch Changes

- Updated dependencies
- ethereum-indexer-js-processor@0.6.17

## 0.0.42

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-conquest-fplay/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-conquest-fplay",
"private": true,
"version": "0.0.42",
"version": "0.0.43",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
7 changes: 7 additions & 0 deletions examples/event-processor-nfts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# event-processor-nfts

## 0.0.43

### Patch Changes

- Updated dependencies
- ethereum-indexer-js-processor@0.6.17

## 0.0.42

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/event-processor-nfts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "event-processor-nfts",
"private": true,
"version": "0.0.42",
"version": "0.0.43",
"description": "",
"type": "module",
"main": "dist/index.cjs",
Expand Down
10 changes: 10 additions & 0 deletions examples/mud/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# web-demo

## 0.1.42

### Patch Changes

- Updated dependencies
- ethereum-indexer-js-processor@0.6.17
- event-processor-bleeps@0.0.43
- event-processor-conquest-eth@0.0.43
- event-processor-nfts@0.0.43

## 0.1.41

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/mud/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mud-demo",
"private": true,
"version": "0.1.41",
"version": "0.1.42",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
8 changes: 8 additions & 0 deletions examples/web-demo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# web-demo

## 0.1.42

### Patch Changes

- event-processor-bleeps@0.0.43
- event-processor-conquest-eth@0.0.43
- event-processor-nfts@0.0.43

## 0.1.41

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/web-demo/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web-demo",
"private": true,
"version": "0.1.41",
"version": "0.1.42",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
6 changes: 6 additions & 0 deletions packages/ethereum-indexer-js-processor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ethereum-indexer-js-processor

## 0.6.17

### Patch Changes

- use Immer instance with autofreeze disabled

## 0.6.16

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/ethereum-indexer-js-processor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-indexer-js-processor",
"version": "0.6.16",
"version": "0.6.17",
"description": "",
"keywords": [],
"author": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
import {logs} from 'named-logs';
import {History, HistoryJSObject} from './history';
import {EventFunctions, JSObject} from './types';
import {Draft, createDraft, finishDraft} from './immer';
import {Draft, Immer} from './immer';

// we do not auto freeze so we can reuse the raw state and modifiy it when needed (if events are known to be immutably included)
const immer = new Immer({autoFreeze: false});

const namedLogger = logs('JSObjectEventProcessor');

Expand Down Expand Up @@ -85,13 +88,6 @@ export class JSObjectEventProcessor<ABI extends Abi, ProcessResultType extends J

async reset() {
namedLogger.info('JSObjectEventProcessor reseting...');
if (!this._json.state) {
throw new Error(`no data`);
}
const keys = Object.keys(this._json.state);
for (const key of keys) {
delete this._json.state[key];
}
const state = this.singleEventProcessor.createInitialState();
this.version = this.singleEventProcessor.version;
const history = {
Expand Down Expand Up @@ -144,13 +140,6 @@ export class JSObjectEventProcessor<ABI extends Abi, ProcessResultType extends J
this._json.history = history;
this.history.setBlock(0, '0x0000');

if (!this._json.state) {
throw new Error(`no data`);
}
const keys = Object.keys(this._json.state);
for (const key of keys) {
delete this._json.state[key];
}
this._json.state = state;
this._json.lastSync = lastSyncFromExistingState;
}
Expand Down Expand Up @@ -196,7 +185,7 @@ export class JSObjectEventProcessor<ABI extends Abi, ProcessResultType extends J
} else {
if (!lastBlockHash || event.blockHash != lastBlockHash) {
if (draft as any) {
const finalizedDraft = finishDraft(draft as any, (_, reversePatches) => {
const finalizedDraft = immer.finishDraft(draft as any, (_, reversePatches) => {
this.history.setReversal(reversePatches);
}) as ProcessResultType;
this._json.state = finalizedDraft as unknown as ProcessResultType;
Expand All @@ -206,7 +195,7 @@ export class JSObjectEventProcessor<ABI extends Abi, ProcessResultType extends J
lastBlock = event.blockNumber;
lastBlockHash = event.blockHash;

draft = createDraft(this._json.state) as Draft<ProcessResultType>;
draft = immer.createDraft(this._json.state) as Draft<ProcessResultType>;
}

this.singleEventProcessor.processEvent(draft as any, event);
Expand All @@ -215,7 +204,7 @@ export class JSObjectEventProcessor<ABI extends Abi, ProcessResultType extends J
}

if (draft as any) {
const finalizedDraft = finishDraft(draft as any, (_, reversePatches) => {
const finalizedDraft = immer.finishDraft(draft as any, (_, reversePatches) => {
this.history.setReversal(reversePatches);
}) as ProcessResultType;
this._json.state = finalizedDraft;
Expand Down

0 comments on commit 4cd00f5

Please sign in to comment.