Skip to content

Commit

Permalink
Change parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklehamster committed Jan 16, 2022
1 parent 201924c commit 90307ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
4 changes: 3 additions & 1 deletion demo/direct-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
};


const directData = new DirectData(localReader, localWriter, null, 500);
const directData = new DirectData({
dataReader: localReader, dataWriter: localWriter, saveAfterMillis: 500,
});
directData.onSave = () => showData(directData);

async function changeData(field, value) {
Expand Down
18 changes: 9 additions & 9 deletions public/direct-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,15 @@ const { DataWriter } = require("./base/data-writer");
const { DataReader } = require("./base/data-reader");

class DirectData {
constructor(dataReader, dataWriter, dataEndPoint, saveAfterMillis) {
constructor(parameters) {
const { fileUtils, dataReader, dataWriter, dataEndPoint, saveAfterMillis, onSave } = parameters || {};
this.dataStore = {};
this.pendingSave = new Set();
this.dataEndPoint = dataEndPoint || "/data";
this.dataWriter = dataWriter || new DataWriter(this.dataEndPoint);
this.dataReader = dataReader || new DataReader(null, this.dataEndPoint);
this.dataReader = dataReader || new DataReader(fileUtils, this.dataEndPoint);
this.saveAfterMillis = saveAfterMillis || 3000;
this.onSave = null;
this.onSave = onSave;
}

async getData(path) {
Expand All @@ -250,12 +251,11 @@ class DirectData {
if (!canWrite) {
return;
}
this.save().then(response => {
console.info(`Save performed. response: ${response}`);
if (this.onSave) {
this.onSave();
}
});
const response = await this.save();
console.info(`Save performed. response: ${response}`);
if (this.onSave) {
this.onSave();
}
}

async save() {
Expand Down
18 changes: 9 additions & 9 deletions src/direct-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ const { DataWriter } = require("./base/data-writer");
const { DataReader } = require("./base/data-reader");

class DirectData {
constructor(dataReader, dataWriter, dataEndPoint, saveAfterMillis) {
constructor(parameters) {
const { fileUtils, dataReader, dataWriter, dataEndPoint, saveAfterMillis, onSave } = parameters || {};
this.dataStore = {};
this.pendingSave = new Set();
this.dataEndPoint = dataEndPoint || "/data";
this.dataWriter = dataWriter || new DataWriter(this.dataEndPoint);
this.dataReader = dataReader || new DataReader(null, this.dataEndPoint);
this.dataReader = dataReader || new DataReader(fileUtils, this.dataEndPoint);
this.saveAfterMillis = saveAfterMillis || 3000;
this.onSave = null;
this.onSave = onSave;
}

async getData(path) {
Expand All @@ -36,12 +37,11 @@ class DirectData {
if (!canWrite) {
return;
}
this.save().then(response => {
console.info(`Save performed. response: ${response}`);
if (this.onSave) {
this.onSave();
}
});
const response = await this.save();
console.info(`Save performed. response: ${response}`);
if (this.onSave) {
this.onSave();
}
}

async save() {
Expand Down
13 changes: 7 additions & 6 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ MockXhr.onSend = (xhr) => {
describe('DirectData', function() {
it('should save data', function(done) {
const fileUtils = new FileUtils(MockXhr);
const directData = new DirectData(
new DataReader(fileUtils, "/test"),
{
const directData = new DirectData({
fileUtils,
dataWriter: {
write: async body => {
expect(body.db1.test).to.equal(123);
return '{"success": true}';
},
},
"/test", 10);

directData.onSave = () => done();
dataEndPoint: "/test",
saveAfterMillis: 10,
onSave: () => done(),
});

directData.getData("db1").then(data1 => {
data1.test = 123;
Expand Down

0 comments on commit 90307ce

Please sign in to comment.