-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60e0e10
commit c606ec7
Showing
11 changed files
with
185 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { STACK_TYPE } from './constants.js' | ||
import { push } from './push.js' | ||
import { dropTable } from './drop-table.js' | ||
import { getTableName } from './helpers.js' | ||
import { db } from './db.js' | ||
|
||
async function changeDirectory({ sessionId, path }) { | ||
await push({ | ||
sessionId, | ||
path, | ||
stackType: STACK_TYPE.UNDO, | ||
}) | ||
const tableName = getTableName({ sessionId, stackType: STACK_TYPE.REDO }) | ||
await dropTable(tableName) | ||
db.close((err) => { | ||
if (err) { | ||
console.error('Error closing the database:', err.message) | ||
} | ||
}) | ||
} | ||
|
||
export { changeDirectory } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { db } from './db.js' | ||
|
||
function dropTable(tableName) { | ||
return new Promise((resolve, reject) => { | ||
db.run(`DROP TABLE IF EXISTS ${tableName}`, (err) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
export { dropTable } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,3 @@ | ||
import { main } from './test.js' | ||
|
||
function end(sessionId) { | ||
console.log('ending ', sessionId) | ||
main().catch((err) => { | ||
console.error('Error:', err.message) | ||
}) | ||
} | ||
function end(sessionId) {} | ||
|
||
export { end } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getTableName = ({ stackType, sessionId }) => { | ||
return `${stackType}_stack_${sessionId}` | ||
} | ||
|
||
export { getTableName } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
function jumpBackward(sessionId) { | ||
console.log('Jumping 1 set back! ', sessionId) | ||
import { STACK_TYPE } from './constants.js' | ||
import { pop } from './pop.js' | ||
import { push } from './push.js' | ||
import { db } from './db.js' | ||
|
||
async function jumpBackward(sessionId) { | ||
const poppedPath = await pop({ sessionId, stackType: STACK_TYPE.UNDO }) | ||
push({ sessionId, path: poppedPath, stackType: STACK_TYPE.REDO }) | ||
db.close((err) => { | ||
if (err) { | ||
console.error('Error closing the database:', err.message) | ||
} | ||
}) | ||
} | ||
|
||
export { jumpBackward } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
function jumpForward(sessionId) { | ||
console.log('Jumping 1 step forward! ', sessionId) | ||
import { pop } from './pop.js' | ||
import { push } from './push.js' | ||
import { STACK_TYPE } from './constants.js' | ||
import { db } from './db.js' | ||
|
||
async function jumpForward(sessionId) { | ||
const poppedPath = await pop({ sessionId, stackType: STACK_TYPE.REDO }) | ||
push({ sessionId, path: poppedPath, stackType: STACK_TYPE.UNDO }) | ||
db.close((err) => { | ||
if (err) { | ||
console.error('Error closing the database:', err.message) | ||
} | ||
}) | ||
} | ||
|
||
export { jumpForward } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { db } from './db.js' | ||
import { getTableName } from './helpers.js' | ||
|
||
async function pop({ sessionId, stackType }) { | ||
const tableName = getTableName({ stackType, sessionId }) | ||
|
||
return new Promise((resolve, reject) => { | ||
db.serialize(() => { | ||
console.log('Popping from, ', tableName) | ||
db.get( | ||
` | ||
SELECT id, path | ||
FROM ${tableName} | ||
ORDER BY id DESC | ||
LIMIT 1 | ||
`, | ||
(err, row) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
|
||
db.run('BEGIN TRANSACTION', (err) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
const { id } = row | ||
db.run( | ||
`DELETE FROM ${tableName} WHERE id = ?`, | ||
[id], | ||
function (err) { | ||
if (err) { | ||
db.run('ROLLBACK', (rollbackErr) => { | ||
if (rollbackErr) { | ||
reject(rollbackErr) | ||
} | ||
}) | ||
reject(err) | ||
} | ||
|
||
db.run('COMMIT', (commitErr) => { | ||
if (commitErr) { | ||
reject(commitErr) | ||
db.run('ROLLBACK', (rollbackErr) => { | ||
reject(rollbackErr) | ||
}) | ||
} | ||
resolve(row.path) | ||
}) | ||
} | ||
) | ||
}) | ||
} | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
export { pop } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters